﻿using System;
using System.Windows;
using System.Windows.Documents;
using Logger;
using Presenter;

namespace VectorsWpf
{
    public partial class MainWindow : Window, Presenter.IView
    {
        public MainWindow()
        {
            InitializeComponent();
            var presenter = new Presenter.Presenter(this, new HtmlLogger("Log.html"));
            Attach(presenter);
        }

        public void Attach(IPresenterCallbacks callback)
        {
            ButtonAdd.Click += (sender, e) => callback.Add();
            ButtonSub.Click += (sender, e) => callback.Sub();
            ButtonDot.Click += (sender, e) => callback.ScalarProduct();
            ButtonNorm.Click += (sender, e) => callback.Norm();
            ButtonNormalize.Click += (sender, e) => callback.Normalize();
            ButtonCross.Click += (sender, e) => callback.CrossProduct();
            ButtonLoadLog.Click += (sender, e) => callback.LoadLog();
        }

        public void SetFirstVector(string x, string y, string z)
        {
            FirstX.Text = x;
            FirstY.Text = y;
            FirstZ.Text = z;
        }

        public void SetSecondVector(string x, string y, string z)
        {
            SecondX.Text = x;
            SecondY.Text = y;
            SecondZ.Text = z;
        }

        public string[] GetFirstVector()
        {
            var fv = new String[3];
            fv[0] = FirstX.Text;
            fv[1] = FirstY.Text;
            fv[2] = FirstZ.Text;
            return fv;
        }

        public string[] GetSecondVector()
        {
            var sv = new String[3];
            sv[0] = SecondX.Text;
            sv[1] = SecondY.Text;
            sv[2] = SecondZ.Text;
            return sv;
        }

        public void ShowError(string error)
        {
            LabelError.Content = error;
        }

        public void HideResults()
        {
            ResScalar.Text = "";
            ResX.Text = "";
            ResY.Text = "";
            ResZ.Text = "";
            LabelError.Content = "";
        }

        public void SetVectorResult(string[] strVector)
        {
            ResX.Text = strVector[0];
            ResY.Text = strVector[1];
            ResZ.Text = strVector[2];
        }

        public void SetScalarResult(string result)
        {
            ResScalar.Text = result;
        }

        public void AddLogRecord(string record)
        {
            var textRange = new TextRange(Log.Document.ContentStart, Log.Document.ContentEnd);
            var Str = textRange.Text+record;
            var paragraph = new Paragraph();
            paragraph.Inlines.Add(new Run(Str));
            Log.Document.Blocks.Add(paragraph);
        }
    }
}
